// pages/store/store.js
const app = getApp()
var host = app.globalData.host;
var self = null;

Page({
  data: {
    //tabbar
    tabbar: {},
    isIphoneX: app.globalData.isIphoneX,
    showAuth: false,

    themeColor: '',
    // banner: null,
    // list1: [], // 餐饮优惠
    // list2: [], // 猜你喜欢
    // list3: [], // 商品
    // list4: [], // 门票
    // list5: [] // 活动门票
    banner: null,
    topList: null,
    lists: []
  },
  onLoad: function() {
    //隐藏系统tabbar
    wx.hideTabBar();
    //tabbar调用
    app.editTabbar();

    self = this;
    self.getStyle();
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          self.init();
        } else {
          wx.setStorageSync("path", self.route);
          // wx.reLaunch({
          //   url: '/pages/login/login'
          // })
          self.setData({
            showAuth: true
          })
        }
      }
    })
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    // var url = host + '/yq/product/banner';
    // var params = {};
    // var success = function (res) {
    //   if (res.info) {
    //     self.setData({
    //       banner: res.info
    //     })
    //   }
    // };
    // var fail = function (res) { };
    // var complete = function () { };
    // app.request(url, params, success, fail, complete);

    // var url1 = host + '/yq/product/discount';
    // var params1 = {};
    // var success1 = function(res) {
    //   self.setData({
    //     list1: res.info
    //   })
    // };
    // var fail1 = function(res) {};
    // var complete1 = function() {};
    // app.request(url1, params1, success1, fail1, complete1);

    // var url2 = host + '/yq/product/recommend';
    // var params2 = {};
    // var success2 = function(res) {
    //   console.log(res);
    //   self.setData({
    //     list2: res.info
    //   })
    // };
    // var fail2 = function(res) {};
    // var complete2 = function() {};
    // app.request(url2, params2, success2, fail2, complete2);

    // var url3 = host + '/yq/product/list';
    // var params3 = {};
    // var success3 = function(res) {
    //   console.log(res);
    //   self.setData({
    //     list3: res.info
    //   })
    // };
    // var fail3 = function(res) {};
    // var complete3 = function() {};
    // app.request(url3, params3, success3, fail3, complete3);

    // var url4 = host + '/yq/product/list';
    // var params4 = {
    //   type: 4
    // };
    // var success4 = function(res) {
    //   console.log(res);
    //   self.setData({
    //     list4: res.info
    //   })
    // };
    // var fail4 = function(res) {};
    // var complete4 = function() {};
    // app.request(url4, params4, success4, fail4, complete4);

    // var url5 = host + '/yq/product/ticket';
    // var params5 = {};
    // var success5 = function(res) {
    //   console.log(res);
    //   self.setData({
    //     list5: res.info
    //   })
    // };
    // var fail5 = function(res) {};
    // var complete5 = function() {};
    // app.request(url5, params5, success5, fail5, complete5);

    var url = host + '/yq/product/initStore';
    var params = {}
    var success = res => {
      console.log(res);
      self.setData({
        banner: res.info.banner ? res.info.banner : null,
        lists: res.info.lists ? res.info.lists : [],
        topList: res.info.topList ? res.info.topList : null
      })
    }
    var fail = res => {
      console.log(res);
    }
    var complete = () => {}
    app.request(url, params, success, fail, complete);
  },
  goInfo(e) {
    var productId = e.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/productInfo/productInfo?productId=' + productId
    })
  },
  goBannerInfo(e) {
    wx.navigateTo({
      url: '/pages/productInfo/productInfo?itemId=' + self.data.banner.id
    })
  },
  goPay(e) {
    if (self.data.info.product.storage == 0) {
      wx.showModal({
        title: '提示',
        content: '补货中',
        showCancel: false
      })
      return;
    }
    // 如果需要填写地址和数量
    // if (self.data.info.product.isNeedMoreInfo === 1) {
    //   wx.navigateTo({
    //     url: '/pages/confirm/confirm?itemId=' + self.data.itemId + '&productId=' + self.data.productId + '&groupId=' + self.data.groupId
    //   })
    //   return;
    // } else if (self.data.info.product.isNeedMoreInfo === 2) {
    //   wx.navigateTo({
    //     url: '/pages/enroll/enroll?itemId=' + self.data.itemId + '&productId=' + self.data.productId + '&groupId=' + self.data.groupId
    //   })
    //   return;
    // }
    if (self.data.info.product.isNeedMoreInfo != 0) {
      wx.navigateTo({
        url: '/pages/extraInfo/extraInfo?itemId=' + self.data.itemId + '&productId=' + self.data.productId + '&groupId=' + self.data.groupId
      })
      return;
    }

    wx.showLoading({
      title: '提交中',
    })
    var productId = e.currentTarget.dataset.productid;
    var url = host + '/yq/order/pay';
    var data = {
      productId: productId
    };
    var success = function(res) {
      console.log(res);
      wx.hideLoading()
      var status = res.status
      if (status == 204) {
        wx.showModal({
          title: '出错了',
          content: res.msg
        })
        return;
      }
      var response = res.info
      var nonce_str = response.nonce_str
      var prepay_id = response.prepay_id
      var sign = response.sign
      var timestamp = response.timeStamp
      var appId = response.appid
      var param = {
        'appId': appId,
        'timeStamp': '' + timestamp,
        'nonceStr': '' + nonce_str,
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': '' + sign,
        'success': function(res) {
          setTimeout(function() {
            wx.reLaunch({
              url: '/pages/success/success?isShowContent=' + 1
            })
          }, 200)

        },
        'fail': function(res) {
          wx.showModal({
            title: '出错了',
            content: '支付失败'
          })
        }
      }
      wx.requestPayment(param);
    };
    var fail = function(res) {};
    var complete = function() {};
    app.request(url, data, success, fail, complete);
  }
})